package com.ctg.payroll2.action.sysadmin;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;

import com.ctg.payroll2.bean.po.SysUser;
import com.ctg.payroll2.service.sysadmin.LoginService;
import com.ctg.payroll2.util.Constants;
import com.ctg.platform.core.security.SpringSecurityManager;
import com.ctg.platform.core.security.entity.Account;
import com.googlecode.jsonplugin.annotations.JSON;
import com.opensymphony.xwork2.ActionSupport;
import com.sun.image.codec.jpeg.ImageFormatException;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class LoginAction extends ActionSupport {
	private static final long serialVersionUID = 7257263192791803146L;
	private SpringSecurityManager springSecurityManager;
	private LoginService loginService;
	private String msg;

	public String execute() {
		return Constants.RELOGIN;
	}

	public String checkLogin() {
		if (ServletActionContext.getRequest().getSession().getAttribute(
				Constants.CURRENT_USER) != null)
			return SUCCESS;
		else
			return Constants.RELOGIN;
	}

	public String checkIsValid() {
		String userName = ServletActionContext.getRequest()
				.getParameter("name");
		if (userName != null) {
			Account a = springSecurityManager.getAccountByUserName(userName,
					null);
			if (a != null && !StringUtils.isEmpty(a.getDisabled())) {
				if (a.getDisabled().equals("1"))
					msg = ERROR;
				else {
					SysUser user = loginService.getCurrentUserByLoginName(a
							.getId(), (SysUser) ServletActionContext
							.getRequest().getSession().getAttribute(
									Constants.CURRENT_USER));
					if (user.getUserStatus().equals(Constants.ACCOUNT_DISABLED)) {
						msg = ERROR;
					} else
						msg = SUCCESS;
				}
			}
		}
		return SUCCESS;
	}

	public String checkMaskCode() {
		String maskCode = ServletActionContext.getRequest().getParameter(
				"validateCode");
		String code = (String) ServletActionContext.getRequest().getSession()
				.getAttribute("rand");
		if (maskCode != null) {
			if (code != null && code.equals(maskCode)) {
				msg = SUCCESS;
			} else
				msg = ERROR;
		}

		return SUCCESS;
	}

	@JSON(serialize = false)
	public String generateCode() {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		String s = "";
		int intCount = 0;
		intCount = (new Random()).nextInt(9999);
		if (intCount < 1000)
			intCount += 1000;
		s = intCount + "";
		request.getSession().setAttribute("rand", s);
		response.setContentType("image/gif");
		ServletOutputStream out = null;
		try {
			out = response.getOutputStream();
		} catch (IOException e) {

			e.printStackTrace();
		}
		// 在内存中创建图象
		int width = 60, height = 20;
		BufferedImage image = new BufferedImage(width, height,
				BufferedImage.TYPE_INT_RGB);
		// 获取图形上下文 �
		Graphics gra = image.getGraphics();
		// 设定背景色
		gra.setColor(new Color(160, 200, 100));
		gra.fillRect(0, 0, width, height);
		// 设定字体
		gra.setFont(new Font("Times New Roman", Font.PLAIN, 18));

		Random random = new Random();
		// 随机产生155条干扰线，使图象中的认证码不易被其它程序探测到
		int r = 160 + random.nextInt(200 - 160);
		int g = 160 + random.nextInt(200 - 160);
		int b = 160 + random.nextInt(200 - 160);
		gra.setColor(new Color(r, g, b));

		for (int i = 0; i < 155; i++) {
			int x = random.nextInt(width);
			int y = random.nextInt(height);
			int xl = random.nextInt(12);
			int yl = random.nextInt(12);
			gra.drawLine(x, y, x + xl, y + yl);
		}

		char c;
		for (int i = 0; i < 4; i++) {
			c = s.charAt(i);
			gra.setColor(new Color(20 + random.nextInt(110), 20 + random
					.nextInt(110), 20 + random.nextInt(110)));
			gra.drawString(c + "", 13 * i + 6, 16);
		}
		JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
		try {
			encoder.encode(image);
			out.close();
		} catch (ImageFormatException e) {

			e.printStackTrace();
		} catch (IOException e) {

			e.printStackTrace();
		}
		return null;
	}

	/**
	 * @param springSecurityManager
	 *            设置springSecurityManager
	 */
	public void setSpringSecurityManager(
			SpringSecurityManager springSecurityManager) {
		this.springSecurityManager = springSecurityManager;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public void setLoginService(LoginService loginService) {
		this.loginService = loginService;
	}
}
